package com.timber.panels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;

import pv.jfcx.JPVCurrency;
import pv.jfcx.JPVDate;
import pv.jfcx.JPVDatePlus;
import pv.jfcx.JPVEdit;
import sun.management.counter.Units;

import com.borland.jbcl.layout.VerticalFlowLayout;
import com.timber.frame.ImsFrame;
import com.timber.util.Global;
import com.timber.util.ITPEdit;
import com.timber.util.ImageMgr;
import com.timber.util.ImsFrameComponent;
import com.timber.util.InventoryMgr;
import com.timber.util.IteamBean;
import com.timber.util.ItemManger;
import com.timber.util.ItemMgrTableModel;
import com.timber.util.MeasurementMgr;
import com.timber.util.SLTablePrinter;
import com.timber.util.SLTableView;
import com.timber.util.SearchableComboBox;
import com.timber.util.StringUtil;
import com.timber.util.SupplierNameMgr;
import com.timber.util.TimberClassMgr;
import com.timber.util.TimberNameMgr;
import com.timber.util.Util;

public class ItemMangerPanel extends ImsFrameComponent {
	
	protected BorderLayout		borderLayout1 = new BorderLayout();
	protected JPanel			topPanel = new JPanel();
	protected GridBagLayout		gridBagLayout1 = new GridBagLayout();
	protected JLabel			classLabel = new JLabel();
	protected JPanel			centerPanel = new JPanel();
	protected Border			border1;
	protected TitledBorder		titledBorder1;
	protected GridBagLayout		gridBagLayout2 = new GridBagLayout();
	protected JLabel			timberNameLabel = new JLabel();
	protected SearchableComboBox		nameCB;
	protected SearchableComboBox		classCB;
	protected SearchableComboBox		supplierCB;
	protected SearchableComboBox		MeasurementCB;
	protected JLabel			recDateFromLabel = new JLabel();
	protected JPVDatePlus			recDateFromDP = new JPVDatePlus();
	protected JPanel			btnPanel = new JPanel();
	protected JPanel			btnHolder = new JPanel();
	protected VerticalFlowLayout		verticalFlowLayout1 = new VerticalFlowLayout();
	protected FlowLayout			flowLayout1 = new FlowLayout();
	protected JButton			clearBtn = new JButton();
	protected JButton			goBtn = new JButton();
	protected ImageIcon			goImage;
	protected ImageIcon			clearAllImage;
	protected ItemMgrTableModel	lnDetailModel = new ItemMgrTableModel();
	
	protected SLTableView			inDetailTV = new SLTableView( lnDetailModel );
	protected SLTablePrinter		aPrinter;
	protected Color			focusCellColor = new Color( 204, 204, 255 );
	private   JMenuItem                   deleteAction  = new JMenuItem("Delete");
	private   JMenuItem                   updateAction  = new JMenuItem("Update");
	
	protected JPVCurrency LengthTF = new JPVCurrency();
	protected JLabel LengthLabel; 
	protected JPVCurrency widthTF = new JPVCurrency();
	protected JLabel 		widthLabel;
	protected JPanel			topPanel2 = new JPanel();
	protected Component			hstrut =  Box.createRigidArea(new Dimension(80,20));
	
	// start
	
	
	
	protected TitledBorder		titledBorder;
	protected SearchableComboBox ItemNameTF =new SearchableComboBox( Global.getMainFrame(),
			TimberNameMgr.mgr.getDataVector(),
			SearchableComboBox.INCLUDE_NONE,
			true );
	
	protected SearchableComboBox ItemClassTF = new SearchableComboBox( Global.getMainFrame(),
			TimberClassMgr.mgr.getDataVector(),
			SearchableComboBox.INCLUDE_NONE,
			true );
	protected JLabel ItemLabel; 
	protected JPVCurrency taxTF = new JPVCurrency();
	protected JLabel taxLabel; 
	protected JPVCurrency qtyTF = new JPVCurrency();
	protected JLabel 		qtyLabel;
	protected JPVCurrency ProfitMarginTF = new JPVCurrency();
	protected JLabel 	totalLabel;
	protected JPVCurrency GrandTotalTF = new JPVCurrency();
	protected JLabel GrandTotalLabel; 
	protected JPanel			woodPanel = new JPanel();
	protected GridBagLayout	 gridBagLayout3=new GridBagLayout();
	protected JButton			addBtn = new JButton();
	protected ImageIcon			addImage;
	Vector currenttimber= new Vector();
	protected Color					focusBG;
	protected Color					nonFocusBG;
	
	protected JLabel			MeasurementLabel = new JLabel();
	protected JLabel			UnitLabel = new JLabel();
	protected JLabel			PurchasePriceLabel = new JLabel();
	protected JLabel			SalesPriceLabel = new JLabel();
	protected JLabel			ProfitMarginLabel = new JLabel();
	protected JLabel			PurchasedFromLabel = new JLabel();
	protected JLabel			PurchasedDate = new JLabel();
	protected JLabel			Location = new JLabel();
	protected JLabel			LotNumber = new JLabel();
	
	protected ITPEdit			PurchasePriceTextBox = new ITPEdit();
	protected JPVCurrency			unitTextTF = new JPVCurrency();;
	protected ITPEdit			SalesPriceTextBox = new ITPEdit();
	protected ITPEdit			ProfitMarginTextBox = new ITPEdit();
	protected ITPEdit			PurchasedFromTextBox = new ITPEdit();
	protected JPVDatePlus		PurchasedDateTextBox = new  JPVDatePlus();
	protected ITPEdit			LocationTextBox = new ITPEdit();
	protected ITPEdit			LotNumberTextBox = new ITPEdit();
	protected ITPEdit itemCodeTF = new ITPEdit();
	protected JPanel			topPanel3 = new JPanel();
	Vector v=new Vector() ;
	protected ImageIcon			saveImage;
	int selectedRow =0;
	// start end
	protected JButton			saveBtn = new JButton();
	
	
	
	public ItemMangerPanel( ImsFrame parent )
	{
		super( parent );
		try
		{
			jbInit();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	void jbInit() throws Exception
	{
		
		
		
		goImage       = ImageMgr.getIcon( ImageMgr.GO32_ICON );
		clearAllImage = ImageMgr.getIcon( ImageMgr.CLEARALL32_ICON );
		saveImage= ImageMgr.getIcon( ImageMgr.RELOAD_ICON );
		this.setLayout( borderLayout1 );
		border1 = BorderFactory.createEtchedBorder( Color.white, new Color( 142, 142, 142 ) );
		topPanel2.setLayout( gridBagLayout3 );
		titledBorder1 = new TitledBorder( border1, " Purchase Details" );
		topPanel.setLayout( gridBagLayout1 );
		classLabel.setText( "Timber Class" );
		centerPanel.setBorder( titledBorder1 );
		centerPanel.setLayout( gridBagLayout2 );
		timberNameLabel.setText( "Timber Name" );
		recDateFromLabel.setText( "From Rec Date" );
		
		topPanel3.setLayout(verticalFlowLayout1);
		this.add(topPanel3, null);
		btnHolder.setLayout( verticalFlowLayout1 );
		
		flowLayout1.setAlignment( FlowLayout.RIGHT );
		btnPanel.setLayout( flowLayout1 );
		
		goBtn.setIcon( goImage );
		goBtn.setMargin( new Insets( 0, 0, 0, 0 ) );
		goBtn.addActionListener( new ActionListener()
				{
			public void actionPerformed( ActionEvent e )
			{
				AddtotableActions( );
			}
			
			
				} );
		
		clearBtn.setIcon( clearAllImage );
		clearBtn.setMargin( new Insets( 0, 0, 0, 0 ) );
		clearBtn.setToolTipText( "Update Item" );
		clearBtn.addActionListener( new ActionListener()
				{
			public void actionPerformed( ActionEvent e )
			{
				updateItemDetails();
			}
			
			private void updateItemDetails() {
				System.out.println("Called updateItemDetails option");
				UppdateActions();
				
			}
				} );
		
		
		nameCB = new SearchableComboBox( Global.getMainFrame(),
				TimberNameMgr.mgr.getDataVector(),
				SearchableComboBox.INCLUDE_ALL,
				true );
		nameCB.setEditable( true );
		nameCB.setPreferredSize( new Dimension( 130, 21 ) );
		
		classCB = new SearchableComboBox( Global.getMainFrame(),
				TimberClassMgr.mgr.getDataVector(),
				SearchableComboBox.INCLUDE_ALL,
				true );
		
		classCB.setEditable( true );
		classCB.setPreferredSize( new Dimension( 130, 21 ) );
		
		
		
		
		supplierCB = new SearchableComboBox( Global.getMainFrame(),
				SupplierNameMgr.mgr.getDataVector(),
				SearchableComboBox.INCLUDE_ALL,
				true );
		
		supplierCB.setEditable( true );
		supplierCB.setPreferredSize( new Dimension( 130, 21 ) );
		
		MeasurementCB= new SearchableComboBox( Global.getMainFrame(),
				MeasurementMgr.mgr.getDataVector(),
				SearchableComboBox.INCLUDE_NONE,
				true );
		
		MeasurementCB.setEditable( true );
		MeasurementCB.setPreferredSize( new Dimension( 130, 21 ) );
		MeasurementCB.addActionListener( new ActionListener()
				{
					public void actionPerformed( ActionEvent e )
					{
						changeNoOfUNits();
					}
					
					private void changeNoOfUNits() {
						System.out.println("Called changeNoOfUNits option");
						
						if(MeasurementCB.getSelectedItem().toString().equalsIgnoreCase("TIMBER"))
						{
							unitTextTF.setDouble(1);
							unitTextTF.setEditable(false);
						}
						
						else
						{
							unitTextTF.setEditable(true);
						}
						
					}
				
				
				} );
		
		
		
		
		recDateFromDP.setDate( null );
		recDateFromDP.setFormat( JPVDate.DMY );
		recDateFromDP.setMonthFormat( JPVDate.MMM );
		recDateFromDP.setSeparator( "-" );
		recDateFromDP.setCloseOnDoubleClick( false );
		recDateFromDP.addFocusListener( new FocusAdapter()
				{
			public void focusGained( FocusEvent fe )
			{
				recDateFromDP.setCaretPosition( 0 );
			}
				} );
		
		
		
		LengthLabel = new JLabel("Length");
		widthLabel = new JLabel("Width"); 
		Dimension d = new Dimension(80,20);
		LengthTF.setDecimals(4);
		LengthTF.setSymbol("");
		LengthTF.setText("");        
		LengthTF.setPreferredSize(d);
		LengthTF.setMinimumSize(d);
		LengthTF.setAlignment(2);
		
		widthTF.setDecimals(4);
		widthTF.setSymbol("");
		widthTF.setText("");        
		widthTF.setPreferredSize(d);
		widthTF.setMinimumSize(d);
		widthTF.setAlignment(2);
		
		unitTextTF.setDecimals(4);
		unitTextTF.setSymbol("");
		unitTextTF.setText("");        
		unitTextTF.setPreferredSize(d);
		unitTextTF.setMinimumSize(d);
		unitTextTF.setAlignment(2);
		
		// start 
		
		ItemLabel  = new JLabel("Timber Code");
		LengthLabel = new JLabel("Length");
		widthLabel = new JLabel("Width"); 
		taxLabel  = new JLabel("Tax");
		qtyLabel=new JLabel("Cubic Qty");
		totalLabel=new JLabel("Total");
		GrandTotalLabel=new JLabel("GrandTotal");
		classLabel.setText( "Timber Type" );
		timberNameLabel.setText( "Timber Name" );
		MeasurementLabel .setText("Measurement");
		PurchasePriceLabel .setText("PurchasePrice");
		SalesPriceLabel .setText("SalesPrice");
		ProfitMarginLabel .setText("ProfitMargin");
		PurchasedFromLabel .setText("PurchasedFrom");
		PurchasedDate .setText("PurchasedDate");
		Location .setText("Location");
		LotNumber .setText("LotNumber");
		ItemNameTF.setName("Timber Name");
		UnitLabel.setText("NoOFUnit");
		
		itemCodeTF.setMaxLength( 9 );
		itemCodeTF.setCase( JPVEdit.UPPER );
		itemCodeTF.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		
		PurchasePriceTextBox.setMaxLength( 9 );
		PurchasePriceTextBox.setCase( JPVEdit.UPPER );
		PurchasePriceTextBox.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		
		
		SalesPriceTextBox.setMaxLength( 9 );
		SalesPriceTextBox.setCase( JPVEdit.UPPER );
		SalesPriceTextBox.setIncludeCharacters( "0123456789" );
		
		ProfitMarginTextBox.setMaxLength( 9 );
		ProfitMarginTextBox.setCase( JPVEdit.UPPER );
		ProfitMarginTextBox.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		PurchasedFromTextBox.setMaxLength( 9 );
		PurchasedFromTextBox.setCase( JPVEdit.UPPER );
		PurchasedFromTextBox.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		PurchasedDateTextBox.setMaxLength( 9 );
		PurchasedDateTextBox.setCase( JPVEdit.UPPER );
		PurchasedDateTextBox.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		LocationTextBox.setMaxLength( 9 );
		LocationTextBox.setCase( JPVEdit.UPPER );
		LocationTextBox.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		LotNumberTextBox.setMaxLength( 9 );
		LotNumberTextBox.setCase( JPVEdit.UPPER );
		LotNumberTextBox.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		
		LengthTF.setDecimals(4);
		LengthTF.setSymbol("");
		LengthTF.setText("");        
		LengthTF.setPreferredSize(d);
		LengthTF.setMinimumSize(d);
		LengthTF.setAlignment(2);
		
		LengthTF.addFocusListener(new java.awt.event.FocusAdapter() {
			
			public void focusLost(FocusEvent e) {
				
				calTimberCubicQty();
				calTotalandTax();
				
			}
		});
		
		
		widthTF.setDecimals(4);
		widthTF.setSymbol("");
		widthTF.setText("");        
		widthTF.setPreferredSize(d);
		widthTF.setMinimumSize(d);
		widthTF.setAlignment(2);
		
		widthTF.addFocusListener(new java.awt.event.FocusAdapter() {
			
			public void focusLost(FocusEvent e) {
				
				calTimberCubicQty();
				calTotalandTax();
				
			}
		});
		
		
		
		taxTF.setSymbol("");
		taxTF.setDecimals(4);
		taxTF.setSymbol("");
		taxTF.setText("");        
		taxTF.setPreferredSize(d);
		taxTF.setMinimumSize(d);
		taxTF.setAlignment(2);
		taxTF.setEditable(false);
		
		ProfitMarginTF.setDecimals(4);
		ProfitMarginTF.setSymbol("");
		ProfitMarginTF.setText("");        
		ProfitMarginTF.setPreferredSize(d);
		ProfitMarginTF.setMinimumSize(d);
		ProfitMarginTF.setAlignment(2);
		ProfitMarginTF.addFocusListener(new java.awt.event.FocusAdapter() {
			
			public void focusLost(FocusEvent e) {
				calTotalandTax();
				
			}
		});
		
		qtyTF.setDecimals(4);
		qtyTF.setSymbol("");
		qtyTF.setText("");        
		qtyTF.setPreferredSize(d);
		qtyTF.setMinimumSize(d);
		qtyTF.setAlignment(2);
		qtyTF.setEditable(true);
		
		
		addBtn.setIcon( addImage );
		addBtn.setMargin( new Insets( 0, 0, 0, 0 ) );
		addBtn.addActionListener( new ActionListener()
				{
			public void actionPerformed( ActionEvent e )
			{
				System.out.println("Clicked Add Button");
			}
			
			
				} );
		
		// item TF action performed
		
		itemCodeTF.setMaxLength( 9 );
		itemCodeTF.setCase( JPVEdit.UPPER );
		itemCodeTF.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		saveBtn.setIcon( saveImage );
		saveBtn.setMargin( new Insets( 0, 0, 0, 0 ) );
		saveBtn.addActionListener( new ActionListener()
				{
			public void actionPerformed( ActionEvent e )
			{
				searchItem( );
			}
			
			
				} );
		
		
		this.add( topPanel3, BorderLayout.NORTH );
		topPanel.add( timberNameLabel,
				new GridBagConstraints( 2,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 5, 0, 0 ), 0, 0 ) );
		topPanel.add( nameCB,
				new GridBagConstraints( 2,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 5, 0, 0 ), 0, 0 ) );
		topPanel.add( classLabel,
				new GridBagConstraints( 3,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 15, 0, 0 ), 0, 0 ) );
		topPanel.add( classCB,
				new GridBagConstraints( 3,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 15, 0, 0 ), 0, 0 ) );
		topPanel.add( ItemLabel,
				new GridBagConstraints( 4,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 40, 0, 0 ), 0, 0 ) );
		topPanel.add( itemCodeTF,
				new GridBagConstraints( 4,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 40, 0, 0 ), 0, 0 ) );
		
		//TODO
		
		
		topPanel.add( LengthLabel,
				new GridBagConstraints( 6,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel.add( LengthTF,
				new GridBagConstraints( 6,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		
		topPanel.add( widthLabel,
				new GridBagConstraints( 7,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel.add( widthTF,
				new GridBagConstraints( 7,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		
		
		topPanel.add( qtyLabel,
				new GridBagConstraints( 8,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel.add( qtyTF,
				new GridBagConstraints( 8,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		
		//		topPanel.add( timberNameLabel,
		//				new GridBagConstraints( 2,
		//						0,
		//						1,
		//						1,
		//						0.0,
		//						0.0,
		//						GridBagConstraints.SOUTHWEST,
		//						GridBagConstraints.NONE,
		//						new Insets( 10, 5, 0, 0 ), 0, 0 ) );
		//		topPanel.add( nameCB,
		//				new GridBagConstraints( 2,
		//						1,
		//						1,
		//						1,
		//						0.0,
		//						0.0,
		//						GridBagConstraints.NORTHWEST,
		//						GridBagConstraints.NONE,
		//						new Insets( 0, 5, 0, 0 ), 0, 0 ) );
		
		
		topPanel.add( MeasurementLabel,
				new GridBagConstraints( 9,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 5, 0, 0 ), 0, 0 ) );
		topPanel.add( MeasurementCB,
				new GridBagConstraints( 9,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 5, 0, 0 ), 0, 0 ) );
		
		
		
		topPanel.add( PurchasePriceLabel,
				new GridBagConstraints( 10,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel.add( PurchasePriceTextBox,
				new GridBagConstraints( 10,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		
		topPanel.add( SalesPriceLabel,
				new GridBagConstraints( 11,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel.add( SalesPriceTextBox,
				new GridBagConstraints( 11,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		
		
		
		
		topPanel.add( btnPanel,
				new GridBagConstraints( 12,
						0,
						GridBagConstraints.REMAINDER,
						GridBagConstraints.REMAINDER,
						100.0,
						100.0,
						GridBagConstraints.CENTER,
						GridBagConstraints.BOTH,
						new Insets( 5, 10, 0, 0 ), 0, 0 ) );
		
		
		
		// second row
		topPanel2.add( hstrut,
				new GridBagConstraints( 1,
						0,
						3,
						2,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 15, 0, 0 ), 0, 0 ) );
		topPanel2.add( UnitLabel,
				new GridBagConstraints( 3,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel2.add( unitTextTF,
				new GridBagConstraints( 3,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		
		topPanel2.add( LotNumber,
				new GridBagConstraints( 4,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel2.add( LotNumberTextBox,
				new GridBagConstraints( 4,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		topPanel2.add( Location,
				new GridBagConstraints( 5,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel2.add( LocationTextBox,
				new GridBagConstraints( 5,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		
		
		topPanel2.add( PurchasedFromLabel,
				new GridBagConstraints( 6,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel2.add( supplierCB,
				new GridBagConstraints( 6,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		
		
		topPanel2.add( PurchasedDate,
				new GridBagConstraints( 7,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel2.add( PurchasedDateTextBox,
				new GridBagConstraints( 7,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		
		
		// old
		topPanel2.add( ProfitMarginLabel,
				new GridBagConstraints( 8,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel2.add( ProfitMarginTF,
				new GridBagConstraints( 8,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		
		topPanel2.add( taxLabel,
				new GridBagConstraints( 9,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel2.add( taxTF,
				new GridBagConstraints( 9,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		topPanel3.add( topPanel,
				new GridBagConstraints(10,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		topPanel3.add( topPanel2,
				new GridBagConstraints(10,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		
		btnPanel.add(btnHolder, null);
		btnHolder.add(goBtn, null);
		btnHolder.add(clearBtn, null);
		btnHolder.add(saveBtn, null);
		
		
		//		    topPanel.add( getSecClass(),
		//		              new GridBagConstraints( 0,
		//		                          1,
		//		                          1,
		//		                          1,
		//		                          0.0,
		//		                          0.0,
		//		                          GridBagConstraints.CENTER,
		//		                          GridBagConstraints.NONE,
		//		                          new Insets( 5, 5, 0, 0 ), 0, 0 ) );
		
		this.add( centerPanel, BorderLayout.CENTER );
		
		deleteAction.addActionListener(new java.awt.event.ActionListener(){
			public void actionPerformed(ActionEvent e) {
				addCorpAction_actionPerformed(e);
			}
			
			private void addCorpAction_actionPerformed(ActionEvent e) {
				System.out.println("Delete event is called ");
				int selectedRow = inDetailTV.getTable().getSelectedRow();
				
				ItemMgrTableModel tm = (ItemMgrTableModel) inDetailTV.getTable().getModel();
				IteamBean iteamBean = (IteamBean) tm.itemAtTableRow(selectedRow);
				
				Util.log( "ItemMgr.Detele is calling()" +iteamBean.getItemCode());
				if (iteamBean.getItemCode()!=null)
				{
					ItemManger.deleteItem(iteamBean.getItemCode());
					
					((ItemMgrTableModel) inDetailTV.getTable().getModel()).removeRow(selectedRow);
				}
				
			}
		});
		updateAction.addActionListener(new java.awt.event.ActionListener(){
			public void actionPerformed(ActionEvent e) {
				updateAction_actionPerformed(e);
			}
			
			private void updateAction_actionPerformed(ActionEvent e) {
				
				DateFormat formatter ; 
				Date date = null ; 
				formatter = new SimpleDateFormat("yy-MM-dd");
				System.out.println("updCorpAction_actionPerformed event is called ");
				selectedRow = inDetailTV.getTable().getSelectedRow();
				System.out.println("Selected Row :"+selectedRow);
				ItemMgrTableModel tm = (ItemMgrTableModel) inDetailTV.getTable().getModel();
				IteamBean iteamBean = (IteamBean) tm.itemAtTableRow(selectedRow);
				itemCodeTF.setText(iteamBean.getItemCode());
				itemCodeTF.setEditable(false);
				nameCB.setSelectedItem(iteamBean.getItemName());
				classCB.setSelectedItem(iteamBean.getItemClass());
				LengthTF.setDouble(iteamBean.getLength());
				widthTF.setDouble(iteamBean.getWidth());
				ProfitMarginTF.setDouble(iteamBean.getProfitMargin());
				taxTF.setDouble(iteamBean.getTax());
				unitTextTF.setDouble(iteamBean.getUnits());
				PurchasePriceTextBox.setText(iteamBean.getPurchasePrice()+"");
				SalesPriceTextBox.setText(iteamBean.getSalesPrice()+"");
				LotNumberTextBox.setText(iteamBean.getLotNumber());
				supplierCB.setSelectedItem(iteamBean.getPurchasedFrom());
				LocationTextBox.setText(iteamBean.getLocation());
				if (iteamBean.getPurchasedDate()!=null)
				{
					try {
						date = (Date)formatter.parse(iteamBean.getPurchasedDate());
					} catch (ParseException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}  
				}
				PurchasedDateTextBox.setDate(date);
				
				qtyTF.setDouble(iteamBean.getQuantity());
				MeasurementCB.setSelectedItem(iteamBean.getMeasurement());
			}
		});
		
		setUpInDetailTV();
		addInDetailTV();
		
		
	}
	
	
	protected void setUpInDetailTV()
	{
		inDetailTV.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
		inDetailTV.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
		inDetailTV.setEditable( false );
		inDetailTV.setEnableSortData( true );
		inDetailTV.setSortingNulls( true );
		inDetailTV.setShowSortIcons( true );
		inDetailTV.setScrollAfterSort( false );
		inDetailTV.getTable().setRowSelectionAllowed( true );
		inDetailTV.getTable().setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
		inDetailTV.setFocusCellBackground( focusCellColor );
		
		inDetailTV.getPopupMenu().add(deleteAction);
		inDetailTV.getPopupMenu().add(updateAction);
		inDetailTV.addRowSelectionListener(new javax.swing.event.ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				// Update security
				int selectedRow = inDetailTV.getTable().getSelectedRow();
				//CorporateAction ca = getSelectedItem(selectedRow);
				
				//	            if (ca != null) {
				//	                if (ca.seid != currSeId) {
				//	                    SecurityObj secObj = SecurityMgr.getSingleSecurity(Integer.toString(ca.seid),"DB_IMS");
				//	                    getSecClass().setClassification(secObj);
				//
				//	                    currSeId = ca.seid;
				//	                }
				//	            }
			}
		});
		
		inDetailTV.getTable().addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent me) {
				if (SwingUtilities.isLeftMouseButton(me)) {
					Point point = me.getPoint();
					
					if (me.getClickCount() == 2) {
						
						//							corpActTblDoSelect(inDetailTV.getTable().rowAtPoint(
						//									point), inDetailTV.getTable().columnAtPoint(
						//									point));
					}
					
				}
			}
		});
		
		
	}
	
	protected void addInDetailTV()
	{
		centerPanel.add( inDetailTV,
				new GridBagConstraints( 0,
						0,
						GridBagConstraints.REMAINDER,
						GridBagConstraints.REMAINDER,
						100.0,
						100.0,
						GridBagConstraints.CENTER,
						GridBagConstraints.BOTH,
						new Insets( 5, 5, 5, 5 ), 0, 0 ) );
		centerPanel.validate();
	}
	
	private void lookupCorpActions() {
		Vector v=new Vector() ;
		System.out.println("Look up corp Action called ");
		InventoryMgr obj =new InventoryMgr();
		v=obj.getInventoryDetails();
		lnDetailModel.setTableData(v);	
		lnDetailModel.fireTableDataChanged();
		
	}
	
	
	
	
	public double calTimberCubicQty()
	{
		// here we are considering the all mesurement in inch
		double qty=0;
		// constant for converting to cubic , it should read from property file 
		double cubiconstant=2304*12;
		if(MeasurementCB.getSelectedItem().toString().equalsIgnoreCase("TIMBER"))
		{
			qty=(LengthTF.getDouble()*widthTF.getDouble()*widthTF.getDouble())/cubiconstant;	 
			qtyTF.setDouble(qty);
		}
		else
		{
			qty =qtyTF.getDouble();
		}
		return qty;
	}
	
	
	public double calTotalandTax()
	{
		double qty=0;
		double toatl=0;
		// should get from property file
		double taxconsant=0.12;
		qty=calTimberCubicQty();
		toatl=qty*ProfitMarginTF.getDouble();
		//totalTF.setDouble(toatl);
		taxTF.setDouble(toatl*taxconsant);
		return toatl;
	}
	
	
	public double calamount()
	{
		double amount=0;
		double toatl=0;
		// should get from property file
		double taxconsant=0.12;
		toatl=calTotalandTax();
		amount=toatl+(toatl*taxconsant);
		System.out.println("Total  :"+toatl);
		System.out.println("Tax :"+toatl*taxconsant);
		System.out.println("amount ---------:"+amount);
		return amount;
	}
	
	private void AddtotableActions() {
		
		ItemManger itemManger = new ItemManger();
		System.out.println("AddtotableActions ");
		IteamBean obj1=new IteamBean();
		// poplauting bean object from Item manager panel
		obj1.setItemCode(itemCodeTF.getText());
		obj1.setItemName(nameCB.getSelectedItem().toString());
		obj1.setItemClass(classCB.getSelectedItem().toString());
		obj1.setLength(LengthTF.getDouble());
		obj1.setWidth(widthTF.getDouble());
		obj1.setProfitMargin(ProfitMarginTF.getDouble());
		obj1.setQuantity(calTimberCubicQty());
		obj1.setTax(taxTF.getDouble());
		obj1.setMeasurement(MeasurementCB.getSelectedItem().toString());
		System.out.println("----------->"+ProfitMarginTF.getText());
		obj1.setProfitMargin(StringUtil.stringtoDouble(ProfitMarginTF.getText()));;
		obj1.setTax(StringUtil.stringtoDouble(taxTF.getText()));
		obj1.setPurchasePrice(StringUtil.stringtoDouble(PurchasePriceTextBox.getText()));
		obj1.setSalesPrice(StringUtil.stringtoDouble(SalesPriceTextBox.getText()));
		obj1.setUnits(unitTextTF.getDouble());
		obj1.setLotNumber(LotNumberTextBox.getText());
		System.out.println("LocationTextBox.getText():"+LocationTextBox.getText());
		obj1.setLocation(LocationTextBox.getText());
		obj1.setPurchasedFrom(supplierCB.getSelectedItem().toString());
		obj1.setPurchasedDate(PurchasedDateTextBox.getText());
		v.add(obj1);
		// add data to database 
		itemManger.insertItem(obj1);
		lnDetailModel.setTableData(v);	
		lnDetailModel.fireTableDataChanged();
		
		clear();
		
	}
	
	private void UppdateActions() {
		
		ItemManger itemManger = new ItemManger();
		System.out.println("UppdateActions ");
		IteamBean obj1=new IteamBean();
		// poplauting bean object from Item manager panel
		obj1.setItemCode(itemCodeTF.getText());
		obj1.setItemName(nameCB.getSelectedItem().toString());
		obj1.setItemClass(classCB.getSelectedItem().toString());
		obj1.setLength(LengthTF.getDouble());
		obj1.setWidth(widthTF.getDouble());
		obj1.setProfitMargin(ProfitMarginTF.getDouble());
		obj1.setQuantity(calTimberCubicQty());
		obj1.setTax(taxTF.getDouble());
		obj1.setMeasurement(MeasurementCB.getSelectedItem().toString());
		obj1.setProfitMargin(StringUtil.stringtoDouble(ProfitMarginTextBox.getText()));;
		obj1.setTax(StringUtil.stringtoDouble(taxTF.getText()));
		obj1.setPurchasePrice(ProfitMarginTF.getDouble());
		obj1.setSalesPrice(StringUtil.stringtoDouble(SalesPriceTextBox.getText()));
		obj1.setLotNumber(LotNumberTextBox.getText());
		obj1.setLocation(LocationTextBox.getText());
		obj1.setPurchasedFrom(supplierCB.getSelectedItem().toString());
		obj1.setPurchasedDate(PurchasedDateTextBox.getText());
		itemCodeTF.setEditable(true);
		// add data to database 
		System.out.println("UppdateActions() Selected Row :"+selectedRow);
		v.add(selectedRow,obj1);
		itemManger.updateItem(obj1);
		System.out.println("UppdateActions() Done");
		lnDetailModel.updateDataRow(obj1,selectedRow);
		lnDetailModel.fireTableDataChanged();
		clear();
		
	}
	
	
	
	
	public void clear()
	{
		itemCodeTF.setText("");
		taxTF.setText("");  
		widthTF.setText("");
		qtyTF.setText("");
		ProfitMarginTF.setText(""); 
		LengthTF.setText("");
	}
	
	
	private void searchItem()
	{
		ItemManger itemManger = new ItemManger();
		System.out.println("searchItem ");
		IteamBean obj1=new IteamBean();
		// poplauting bean object from Item manager panel
		obj1.setItemCode(itemCodeTF.getText());
		obj1.setItemName(nameCB.getSelectedItem().toString());
		obj1.setItemClass(classCB.getSelectedItem().toString());
		obj1.setLength(LengthTF.getDouble());
		obj1.setWidth(widthTF.getDouble());
		obj1.setProfitMargin(ProfitMarginTF.getDouble());
		obj1.setQuantity(calTimberCubicQty());
		obj1.setTax(taxTF.getDouble());
		obj1.setMeasurement(MeasurementCB.getSelectedItem().toString());
		obj1.setProfitMargin(StringUtil.stringtoDouble(ProfitMarginTextBox.getText()));;
		obj1.setTax(StringUtil.stringtoDouble(taxTF.getText()));
		obj1.setPurchasePrice(ProfitMarginTF.getDouble());
		obj1.setSalesPrice(StringUtil.stringtoDouble(SalesPriceTextBox.getText()));
		obj1.setLotNumber(LotNumberTextBox.getText());
		obj1.setLocation(LocationTextBox.getText());
		obj1.setPurchasedFrom(supplierCB.getSelectedItem().toString());
		obj1.setPurchasedDate(PurchasedDateTextBox.getText());
		// add data to database 
		if(obj1.getItemCode()!=null)
		{
		v= itemManger.searchItem(obj1);
		lnDetailModel.setTableData(v);
		lnDetailModel.fireTableDataChanged();
		}
		clear();
		System.out.println("search action called ");
		
	}
	
	
	
	
}
